<?php
session_start();
require_once("Structure/fonctions.php");

$PAGE_TITLE = "Inscription - Lolgames";
$CONTENT_INCLUDE = "inscription.html";

if(isset($_POST['valider'])) {
    $nom = $_POST['nom'];
    $prenom= $_POST['prenom'];
    $email = $_POST['email'];
    $pseudo = $_POST['pseudo'];
    $password = sha1($_POST['password']);
    $password2 = sha1($_POST['password2']);

    $bdd = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_BASE);
	
	include("Structure/updateUser.php");

    if (strcmp($password, $password2) == 0) { // On vérifie que les 2 pwd sont identiques
        $query = "SELECT  id  FROM    users
                  WHERE   pseudo = '".$pseudo."' ;";
        $double = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
        if(mysqli_num_rows($double) != 0) {
            $error = "Pseudo";
        }

        $query = "SELECT  id  FROM    users
                  WHERE   email = '".$email."' ;";
        $double2 = mysqli_query($bdd, $query) or die(mysqli_error($bdd));
        if(mysqli_num_rows($double2) != 0) {
            if(mysqli_num_rows($double) != 0) {
                $error = "Pseudo et e-mail";
            }
            else {
                $error = "E-mail";
            }
        }

        if(!isset($error)) {
            $query = "INSERT INTO   users (lastName, firstName, email, pseudo, password, roleID, statusID, hireDate)
                      VALUES        ('".$nom."', '".$prenom."', '".$email."', '".$pseudo."', '".$password."', 0, 10, SYSDATE());";
            $res = mysqli_query($bdd, $query) or die(mysql_error($bdd));
            header("Location: connexion.php");
            exit;
        }
    }
    else {
        $error_pwd = "Les mots de passe sont différent !";
    }
}

include_once("Structure/structure.php");
?>
